# -*- coding: utf-8 -*-
from shlex import split
from subprocess import Popen, PIPE

def snmpset(community: str = "public",file_path:str = None, host: str = None, OIDname: str = None, set_type: str = None, set_value: float = None):
    command = "snmpset -v 2c -c {} -m {} {} {} {} {}".format(
        community, file_path, host, OIDname, set_type, set_value)
    #print(command)
    try:
        proc = Popen(split(
            command), shell=False, stdout=PIPE, stderr=PIPE)
        proc.wait(timeout=0.1)
        if proc.poll() == 0:
            msg = proc.stdout.read().decode("gb2312")
        else:
            msg = proc.stderr.read().decode("gb2312")
    except:
        msg = "你电脑装SNMP了嘛  (╬▔皿▔)╯  你就用!!!,快去安装SNMP命令行工具"
    return str(msg)


def snmpget(community: str = "public", file_path:str = None,host: str = None, OIDname: str = ""):
    # command = "echo {} {} {}".format(
    #     community, mask, set_value)
    # proc = Popen(split(
    #     command), shell=True, stdout=PIPE, stderr=PIPE)
    command = "snmpwalk -v 2c -c {} -m {} {} {}".format(
        community, file_path, host, OIDname)
    #print(command)
    try:
        proc = Popen(split(
            command), shell=False, stdout=PIPE, stderr=PIPE)
        proc.wait(timeout=0.1)
        if proc.poll() == 0:
            msg = proc.stdout.read().decode("gb2312")

        else:
            msg = proc.stderr.read().decode("gb2312")
    except:
        msg = "你电脑装SNMP了嘛  (╬▔皿▔)╯  你就用!!!,快去安装SNMP命令行工具"
    # print(msg)
    return str(msg)

    
